package com.zhuoyue.system.service.impl;

import java.util.Collections;
import java.util.List;

import com.github.pagehelper.PageHelper;
import com.zhuoyue.common.annotation.DataScope;
import com.zhuoyue.common.core.page.PageDomain;
import com.zhuoyue.common.core.page.TableSupport;
import com.zhuoyue.common.utils.DateUtils;
import com.zhuoyue.common.utils.StringUtils;
import com.zhuoyue.common.utils.sql.SqlUtil;
import com.zhuoyue.system.domain.TblDevice;
import com.zhuoyue.system.domain.TblGateway;
import com.zhuoyue.system.domain.TblGroup;
import com.zhuoyue.system.domain.vo.ZigbeeDeviceLinkageRelationVo;
import com.zhuoyue.system.mapper.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.zhuoyue.system.domain.ZigbeeDeviceLinkageRelation;
import com.zhuoyue.system.service.IZigbeeDeviceLinkageRelationService;
import org.springframework.util.CollectionUtils;

/**
 * 极享设备联动配置Service业务层处理
 *
 * @author zyzn
 * @date 2021-09-24
 */
@Service
public class ZigbeeDeviceLinkageRelationServiceImpl implements IZigbeeDeviceLinkageRelationService {
    @Autowired
    private ZigbeeDeviceLinkageRelationMapper zigbeeDeviceLinkageRelationMapper;
    @Autowired
    private TblDeviceMapper deviceMapper;
    @Autowired
    private TblGatewayMapper gatewayMapper;
    @Autowired
    private TblGroupMapper groupMapper;

    private static final String DEVICE = "device";
    private static final String GROUP = "group";

    /**
     * 查询极享设备联动配置
     *
     * @param id 极享设备联动配置ID
     * @return 极享设备联动配置
     */
    @Override
    public ZigbeeDeviceLinkageRelation selectZigbeeDeviceLinkageRelationById(String id) {
        return zigbeeDeviceLinkageRelationMapper.selectZigbeeDeviceLinkageRelationById(id);
    }

    /**
     * 查询极享设备联动配置列表
     *
     * @param params 极享设备联动配置
     * @return 极享设备联动配置
     */
    @DataScope(deptAlias = "s_d")
    @Override
    public List<ZigbeeDeviceLinkageRelationVo> selectZigbeeDeviceLinkageRelationList(ZigbeeDeviceLinkageRelation params) {
        List<ZigbeeDeviceLinkageRelationVo> zigbeeDeviceLinkageRelationVos = null;
        // 查询设备ID列表
        if (StringUtils.isNotEmpty(params.getDeviceName()) || StringUtils.isNotEmpty(params.getDeviceMac())) {
            TblDevice device = new TblDevice();
            device.setName(params.getDeviceName());
            device.setZigbeeMac(params.getDeviceName());
            List<String> devices = deviceMapper.selectDeviceMessage(device);
            params.setDeviceIds(devices);

        }
        // 查询群组ID列表
        if (StringUtils.isNotEmpty(params.getDeviceName())) {
            TblGroup group = new TblGroup();
            group.setName(params.getDeviceName());
            List<String> groups = groupMapper.selectGroupMessage(group);
            params.setGroupIds(groups);
        }
        // 查询网关ID列表
        if (StringUtils.isNotEmpty(params.getGatewayMac())) {
            TblGateway gateway = new TblGateway();
            gateway.setZigbeeMac(params.getGatewayMac());
            List<String> gateways = gatewayMapper.selectGatewayMessage(gateway);
            params.setGatewayMacs(gateways);
        }
        // 是否有查到设备ID, 群组ID, 网关mac列表
        Boolean isTrue = !CollectionUtils.isEmpty(params.getDeviceIds()) || !CollectionUtils.isEmpty(params.getGatewayMacs()) || !CollectionUtils.isEmpty(params.getGroupIds());
        // 若按网关和设备名称或mac查询不到数据, 则直接返回空列表, 不需要查数据库
        if (!isTrue) {
            if (StringUtils.isNotEmpty(params.getDeviceName()) || StringUtils.isNotEmpty(params.getGatewayMac())) {
                return Collections.emptyList();
            }
        }

//        if (params.getEndType() != null) {
//            isTrue = true;
//        }

        startPage();
        if (isTrue) {
            zigbeeDeviceLinkageRelationVos = zigbeeDeviceLinkageRelationMapper.selectZigbeeDeviceLinkageRelationListParams(params);
        } else {
            zigbeeDeviceLinkageRelationVos = zigbeeDeviceLinkageRelationMapper.selectZigbeeDeviceLinkageRelationList(params);
        }

        // 查询主设备和从设备(zigbee类型)的类型名称
        if (!CollectionUtils.isEmpty(zigbeeDeviceLinkageRelationVos)) {
            zigbeeDeviceLinkageRelationVos.forEach(i -> {
                i.setStartDeviceType(i.getStartOdIndex() + i.getStartDeviceType() + i.getStartProductType());
                String startName = zigbeeDeviceLinkageRelationMapper.selectDeviceName(StringUtils.substring(i.getStartDeviceType(), 0, 8));
                i.setStartCategoryName(startName);
                if (DEVICE.equals(i.getEndType())) {
                    i.setEndDeviceType(i.getEndOdIndex() + i.getEndDeviceType() + i.getEndProductType());
                    String endName = zigbeeDeviceLinkageRelationMapper.selectDeviceName(StringUtils.substring(i.getEndDeviceType(), 0, 8));
                    i.setEndCategoryName(endName);
                }
            });
        }
        return zigbeeDeviceLinkageRelationVos;
    }

    /**
     * 新增极享设备联动配置
     *
     * @param zigbeeDeviceLinkageRelation 极享设备联动配置
     * @return 结果
     */
    @Override
    public int insertZigbeeDeviceLinkageRelation(ZigbeeDeviceLinkageRelation zigbeeDeviceLinkageRelation) {
        zigbeeDeviceLinkageRelation.setCreateTime(DateUtils.getNowDate());
        return zigbeeDeviceLinkageRelationMapper.insertZigbeeDeviceLinkageRelation(zigbeeDeviceLinkageRelation);
    }

    /**
     * 修改极享设备联动配置
     *
     * @param zigbeeDeviceLinkageRelation 极享设备联动配置
     * @return 结果
     */
    @Override
    public int updateZigbeeDeviceLinkageRelation(ZigbeeDeviceLinkageRelation zigbeeDeviceLinkageRelation) {
        return zigbeeDeviceLinkageRelationMapper.updateZigbeeDeviceLinkageRelation(zigbeeDeviceLinkageRelation);
    }

    /**
     * 批量删除极享设备联动配置
     *
     * @param ids 需要删除的极享设备联动配置ID
     * @return 结果
     */
    @Override
    public int deleteZigbeeDeviceLinkageRelationByIds(String[] ids) {
        return zigbeeDeviceLinkageRelationMapper.deleteZigbeeDeviceLinkageRelationByIds(ids);
    }

    /**
     * 删除极享设备联动配置信息
     *
     * @param id 极享设备联动配置ID
     * @return 结果
     */
    @Override
    public int deleteZigbeeDeviceLinkageRelationById(String id) {
        return zigbeeDeviceLinkageRelationMapper.deleteZigbeeDeviceLinkageRelationById(id);
    }

    @Override
    public int count() {
        return zigbeeDeviceLinkageRelationMapper.selectCount();
    }

    protected void startPage() {
        PageDomain pageDomain = TableSupport.buildPageRequest();
        Integer pageNum = pageDomain.getPageNum();
        Integer pageSize = pageDomain.getPageSize();
        if (StringUtils.isNotNull(pageNum) && StringUtils.isNotNull(pageSize)) {
            String orderBy = SqlUtil.escapeOrderBySql(pageDomain.getOrderBy());
            PageHelper.startPage(pageNum, pageSize, orderBy);
        }
    }
}
